<template>
  <div class="user-edit">
    <!-- Drawer抽屉 -->
    <Drawer :title="title" v-model="visible" width="720" draggable :mask-closable="type=='0'">
      <div :style="{maxHeight: maxHeight}" class="drawer-content">
        <div class="user-title">
          <div class="info-title">基本信息</div>
          <Avatar :src="form.avatar" size="large" v-show="type!=='2'" />
        </div>
        <Form label-colon v-show="type!=='2'">
          <Row :gutter="32">
            <Col span="12">
              <FormItem label="用户ID">
                {{form.id}}
                <Tooltip trigger="hover" placement="right" content="账户已禁用">
                  <Icon
                    v-show="form.status===-1"
                    type="md-lock"
                    size="18"
                    style="margin-left:10px;cursor:pointer"
                  />
                </Tooltip>
              </FormItem>
            </Col>
            <Col span="12">
              <FormItem label="登录账号">
                {{form.username}}
                <Tooltip trigger="hover" placement="right" :content="`密码强度：${form.passStrength}`">
                  <Icon
                    v-show="form.passStrength"
                    type="md-key"
                    :color="passColor"
                    size="18"
                    style="margin-left:10px;cursor:pointer"
                  />
                </Tooltip>
              </FormItem>
            </Col>
          </Row>
        </Form>
        <Form ref="form" :model="form" :rules="formValidate" label-position="top">
          <Row :gutter="32" v-if="type==='2'">
            <Col span="12">
              <FormItem label="用户名" prop="nickname">
                <Input v-model="form.nickname" />
              </FormItem>
            </Col>
            <Col span="12">
              <FormItem label="密码" prop="password">
                <SetPassword v-model="form.password" @on-change="changePass" />
              </FormItem>
            </Col>
          </Row>
          <Row :gutter="32">
            <Col span="12">
              <FormItem label="手机号" prop="mobile">
                <Input v-model="form.mobile" />
              </FormItem>
            </Col>
            <Col span="12">
              <FormItem label="性别">
                <Select v-model="form.sex">
                  <Option
                    v-for="(item, i) in this.$store.state.dict.sex"
                    :key="i"
                    :value="item.value"
                  >{{item.title}}</Option>
                </Select>
              </FormItem>
            </Col>
          </Row>
          <Row :gutter="32">
            <Col span="12">
              <FormItem label="邮箱" prop="email">
                <Input v-model="form.email" />
              </FormItem>
            </Col>
            <Col span="12">
              <FormItem label="单位代码" prop="dutyCode">
                <Input v-model="form.dutyCode" />
              </FormItem>
            </Col>
          </Row>
          <Row :gutter="32">
            <Col span="12">
              <FormItem label="头像">
                <upload-pic-input v-model="form.avatar"></upload-pic-input>
              </FormItem>
            </Col>
            <Col span="12">
              <Form-item label="区域" prop="areaId" v-model="form.areaId">
                <area-choose @on-change="handleSelectAreaChoose" style="width: 200px" ref="areaId"></area-choose>
              </Form-item>
            </Col>
           <!-- <Col span="12">
              <FormItem label="角色分配" prop="roleIds">
                <Select v-model="form.roleIds" multiple>
                  <Option
                    v-for="item in roleList"
                    :value="item.id"
                    :key="item.id"
                    :label="item.name"
                  >
                    <span style="margin-right:10px;">{{ item.name }}</span>
                    <span style="color:#ccc;">{{ item.description }}</span>
                  </Option>
                </Select>
              </FormItem>
            </Col>-->
          </Row>
          <Divider />
          <p class="info-header">监管部门账号资料</p>
          <Row :gutter="32">
            <Col span="12">
              <Form-item label="行业" prop="businessId">
                <Select v-model="form.businessId" placeholder="请选择" clearable style="width: 200px">
                  <Option v-for="(item, i) in businessData" :key="i" :value="item.id">{{item.businessName}}</Option>
                </Select>
              </Form-item>
            </Col>
            <Col span="12">
              <Form-item label="科室" prop="officeId">
                <Select v-model="form.officeId" placeholder="请选择" clearable style="width: 200px">
                  <Option v-for="(item, i) in officeData" :key="i" :value="item.id">{{item.officeName}}</Option>
                </Select>
              </Form-item>
            </Col>
          </Row>
          <Row :gutter="32">
            <Col span="12">
              <FormItem label="职务" prop="dutyName">
                <Input v-model="form.dutyName" />
              </FormItem>
            </Col>
          </Row>
        </Form>
      </div>
      <div class="drawer-footer br" v-show="type!=='0'">
        <Button type="primary" :loading="submitLoading" @click="submit">提交</Button>
        <Button @click="visible = false">取消</Button>
      </div>
    </Drawer>
  </div>
</template>

<script>
import { getAllRoleList, getOfficeVoList, createSupervisionUser, updateSupervisionUser, getBusinessList } from "@/api/index";
import {
  validateUsername,
  validateMobile,
  validatePassword
} from "@/libs/validate";
import departmentTreeChoose from "@/views/my-components/xboot/department-tree-choose";
import uploadPicInput from "@/views/my-components/xboot/upload-pic-input";
import SetPassword from "@/views/my-components/xboot/set-password";
import AreaChoose from "../../my-components/xboot/area-choose";
import DepartmentChoose from "../../my-components/xboot/department-choose";
export default {
  name: "user",
  components: {
    DepartmentChoose,
    AreaChoose,
    departmentTreeChoose,
    uploadPicInput,
    SetPassword
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object
    },
    type: {
      type: String,
      default: "0"
    }
  },
  data() {
    return {
      departmentAccount: true,
      officeData: [],
      businessData: [],
      dataList: [
        {
         id: 1,
         name: "省级部门"
        },
        {
          id: 2,
          name: "市级部门"
        },
        {
          id: 3,
          name: "区县部门"
        },
        {
          id: 4,
          name: "乡镇部门"
        }
      ],
      visible: this.value,
      title: "",
      passColor: "",
      submitLoading: false,
      maxHeight: 510,
      form: {
        addressArray: [],
        departmentAccount: true
      },
      formValidate: {
        // 表单验证规则
        username: [
          { required: true, message: "请输入登录账号", trigger: "blur" },
          { validator: validateUsername, trigger: "blur" }
        ],
        nickname: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { validator: validateMobile, trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { validator: validatePassword, trigger: "blur" }
        ],
      },
      roleList: []
    };
  },
  methods: {
    init() {
      this.getRoleList();
      this.getBusinessList();
    },
    getRoleList() {
      getAllRoleList().then(res => {
        if (res.success) {
          this.roleList = res.result;
        }
      });
    },

    getBusinessList() {
      getBusinessList().then(res => {
        if (res.success) {
          this.businessData = res.result.content;
        }
      });
    },
    handleSelectDepTree(v) {
      this.form.departmentId = v;
    },
    handleSelectAreaChoose(v) {
      this.form.areaId = v;
    },
    changePass(v, grade, strength) {
      this.form.passStrength = strength;
    },
    getOfficeData() {
      getOfficeVoList().then(res => {
        console.log(res.result);
        if (res.success) {
          this.officeData = res.result;
        }
      });
    },
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.type === "1") {
            // 编辑
            this.submitLoading = true;
            this.form.departmentAccount = true;
            updateSupervisionUser(this.form).then(res => {
              this.submitLoading = false;
              if (res.success) {
                this.$Message.success("操作成功");
                this.$emit("on-submit", true);
                this.visible = false;
              }
            });
          } else {
            // 添加
            this.submitLoading = true;
            this.form.departmentAccount = true;
            createSupervisionUser(this.form).then(res => {
              this.submitLoading = false;
              if (res.success) {
                this.$Message.success("操作成功");
                this.$emit("on-submit", true);
                this.visible = false;
              }
            });
          }
        }
      });
    },
    setCurrentValue(value) {
      if (value === this.visible) {
        return;
      }
      if (this.type === "1") {
        this.title = "编辑监管部门用户";
        this.maxHeight = Number(document.documentElement.clientHeight - 121) + "px";
      } else if (this.type === "2") {
        this.title = "添加监管部门用户";
        this.maxHeight = Number(document.documentElement.clientHeight - 121) + "px";
      } else {
        this.title = "监管部门用户详情";
        this.maxHeight = "100%";
      }
      // 清空数据
      this.$refs.form.resetFields();
      if (this.type === "0" || this.type === "1") {
        // 回显数据
        let data = this.data;
        // 地址
        if (data.address) {
          data.addressArray = JSON.parse(data.address);
        } else {
          data.addressArray = [];
        }
        // 角色
        let selectRolesId = [];
        data.roles.forEach(function(e) {
          selectRolesId.push(e.id);
        });
        data.roleIds = selectRolesId;
        delete data.roles;
        //delete data.permissions;
        // 密码强度
        if (data.passStrength === "弱") {
          this.passColor = "#ed3f14";
        } else if (data.passStrength === "中") {
          this.passColor = "#faad14";
        } else if (data.passStrength === "强") {
          this.passColor = "#52c41a";
        }
        // 回显
        this.form = data;
      } else {
        this.form = {
          type: 0,
          sex: "",
          addressArray: []
        };
      }
      this.visible = value;
    }
  },
  watch: {
    value(val) {
      this.setCurrentValue(val);
    },
    visible(value) {
      this.$emit("input", value);
    }
  },
  mounted() {
    this.init();
    this.getOfficeData();
  }
};
</script>

<style lang="less">
@import "../../../styles/table-common.less";
.drawer-content {
  overflow: auto;
}
.drawer-content::-webkit-scrollbar {
  display: none;
}
.user-title {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
  .info-title {
    font-size: 16px;
    color: rgba(0, 0, 0, 0.85);
    display: block;
    margin-right: 16px;
  }
}
.info-header {
  font-size: 16px;
  color: rgba(0, 0, 0, 0.85);
  display: block;
  margin-bottom: 16px;
}
</style>

